package com.tanhua.controller;

import com.tanhua.model.mogo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/movements")
public class MovementsController {
    @Autowired
    private MovementsService movementsService;

    /**
     * 发布动态
     * @param movement
     * @param imageContent
     * @return
     * @throws IOException
     */
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile[] imageContent) throws IOException {
        movementsService.movements(movement,imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询个人动态
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity FindByUserId(Long userId,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10")Integer pagesize){
        PageResult pr = movementsService.FindByUserId(userId,page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查询好友动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity FindByFridends( @RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10")Integer pagesize){
        PageResult pr = movementsService.FindByFridends(page,pagesize);
        return ResponseEntity.ok(pr);
    }


    /**
     * 查询推荐动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity  RecommendMovements(@RequestParam(defaultValue = "1") Integer page,
                                              @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr = movementsService.RecommendMovements(page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 根据动态id查询单个动态
     * @param movementId
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity FindById(@PathVariable("id") String movementId){
        MovementsVo vo = movementsService.FindById(movementId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 动态点赞
     * @param id 动态id
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable Integer id){
        Integer count = movementsService.like(id);
        return ResponseEntity.ok(count);
    }
}
